<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2019/4/25
 * Time: 15:33
 */
namespace app\api\controller\goods;

use app\api\controller\BasicApi;
use think\Db;

class Goods extends BasicApi
{
    /**
     * 产品列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function lists(){
        $page_now=input('page_now',1);
        $page_size=input('page_size',10);
        $keyword=input('keyword');

        $order_arr[0]='gl.goods_sale';//销量：gl.goods_sale
        $order_arr[1]='g.create_at';//上架时间：g.create_at
        $order_arr[2]='gl.selling_price';//积分数量：gl.selling_price
        $order_arr[3]='gl.goods_stock';//商品库存：gl.goods_stock

        $order=input('order/d',0);//排序方式
        $is_desc=input('sort/d',0);//是否升序
        $order_str=$order_arr[$order].' '.($is_desc?'desc':'asc').',g.sort';

        $db=Db::name('store_goods')
            ->alias('g')
            ->join('store_goods_list gl','g.id=gl.goods_id')
            ->where('gl.goods_stock','>',0)
            ->where('g.status',1)
            ->where('g.is_deleted',0);

        if($keyword){
            $db->where('goods_title','like','%'.$keyword.'%');
        }

        $list=$db->page($page_now,$page_size)
            ->order($order_str)
            ->field('g.id,g.goods_logo,g.goods_title,gl.selling_price')
            ->select();

        return json(['msg'=>'success','data'=>$list]);
    }

    /**
     * 商品详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function view(){
        $id=input('id/d');
        //查询商品详情
        $goods=Db::name('store_goods')
            ->alias('g')
            ->join('store_goods_list gl','gl.goods_id=g.id')
            ->where('g.id',$id)
            ->field('g.id,g.goods_image,g.goods_title,g.goods_content,gl.goods_stock,gl.selling_price')
            ->find();
        $goods['goods_image']=explode('|',$goods['goods_image']);

        return json(['msg'=>'success','data'=>$goods]);
    }
}